package org.jeecg.chase.common.engine.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.chase.common.base.annotation.QueryConfig;
import org.jeecg.chase.common.base.data.BaseQuery;
import org.jeecg.chase.common.script.dao.CommonDataDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author leikai
 * @version 1.0
 * @description:
 * @date 2021/07/26/0026 14:35
 */
@Transactional
public abstract class AbstractCommonDataService extends AbstractDataEngine {

    @Autowired
    private CommonDataDao commonDataDao;

    /**
     * 获取返回对象
     *
     * @param query
     * @return java.lang.Class<?>
     * @author leikai
     * @date 2021/07/26/0026 14:36
     */
    @Override
    public Class<?> getResultClass(BaseQuery query) {
        Class<?> baseQueryClazz = query.getClass();
        QueryConfig queryConfig = (QueryConfig) baseQueryClazz.getAnnotation(QueryConfig.class);
        if (null == queryConfig) {
            throw new RuntimeException(baseQueryClazz.getSimpleName() + "没有配置@QueryConfig注解");
        }
        Class<?> resultClass = queryConfig.resultClass();
        if (Map.class.equals(resultClass)) {
            resultClass = null;
        }
        return resultClass;
    }

    /**
     * 列表查询
     *
     * @param query
     * @param resultClass
     * @return java.util.List<?>
     * @author leikai
     * @date 2021/07/26/0026 17:50
     */
    @Override
    public List<?> doGetList(BaseQuery query, Class<?> resultClass) {
        return this.commonDataDao.getList(query, resultClass);
    }

    /**
     * 分页查询
     *
     * @param query
     * @param resultClass
     * @return org.jeecg.chase.common.dataaccess.data.Page<?>
     * @author leikai
     * @date 2021/07/26/0026 17:51
     */
    @Override
    public Page<?> doGetPage(BaseQuery query, Class<?> resultClass) {
        return this.commonDataDao.getPage(query, resultClass);
    }

}
